/*
 * @Author: Eureka 
 * @Date: 2020-09-10 10:50:58 
 * @Last Modified by: Eureka
 * @Last Modified time: 2021-05-11 14:53:35
 */

import 'package:flutter/material.dart';

class RouterUtil {
  ///动态路由 - 跳转
  static Future<void> pushPage(BuildContext context, Widget cls,
      {Function? popResult}) async {
    Navigator.of(context).push(MaterialPageRoute(builder: (_) {
      return cls;
    })).then((dynamic value) => () {
          if (value != null || popResult != null) {
            popResult!(value);
          }
        });
  }

  //移除当前页跳转 A --- B --- C  C --- A
  static Future<void> pushReplacement(BuildContext context, Widget cls,
      {Function? popResult}) async {
    Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (_) {
      return cls;
    })).then((dynamic value) => () {
          if (value != null || popResult != null) {
            popResult!(value);
          }
        });
  }

  ///动态路由 - 跳转
  static rootPushPage(BuildContext context, Widget cls,
      {Function? popResult}) async {
    Navigator.of(context, rootNavigator: true)
        .push(MaterialPageRoute(builder: (_) {
      return cls;
    })).then((dynamic value) => () {
              if (value != null || popResult != null) {
                popResult!(value);
              }
            });
  }

  ///动态路由 - 跳转
  static pushRemovePage(BuildContext context, Widget cls,
      {Function? popResult}) {
    Navigator.of(context)
        .pushAndRemoveUntil(
          MaterialPageRoute(builder: (_) => cls),
          (Route<dynamic> route) => false,
        )
        .then((dynamic value) => () {
              if (value != null || popResult != null) {
                popResult!(value);
              }
            });
  }

  ///静态路由 - 跳转
  static pushName(BuildContext context, String path, {Function? popResult}) {
    Navigator.of(context).pushNamed(path).then((value) {
      if (value != null || popResult != null) {
        popResult!(value);
      }
    });
  }

  // static pushImagePage(BuildContext context, Widget page) {
  //   Navigator.push(
  //     context,
  //     AppConfig.isAndroid
  //         ? TransparentMaterialPageRoute(builder: (_) => page)
  //         : TransparentCupertinoPageRoute(builder: (_) => page),
  //   );
  // }

  ///退出单个页面
  static popPage(BuildContext context, {dynamic params}) {
    if (Navigator.canPop(context)) {
      Navigator.pop(context, params);
    }
  }

  ///退出多个页面-静态方法
  static popMultiplePage(BuildContext context, String path) {
    Navigator.popUntil(context, ModalRoute.withName(path));
  }

  ///退出到根目录
  static popRootPage(BuildContext context) {
    while (Navigator.canPop(context)) {
      Navigator.pop(context);
    }
  }

  ///退出指定层级页面
  static popLevelsPage(BuildContext context, int popNum) {
    for (int i = 0; i < popNum; i++) {
      if (Navigator.canPop(context)) {
        Navigator.pop(context);
      }
    }
  }
}

class SlideTopRoute extends PageRouteBuilder {
  final Widget? page;
  SlideTopRoute({this.page})
      : super(
          pageBuilder: (
            BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
          ) =>
              page!,
          transitionsBuilder: (
            BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
            Widget child,
          ) =>
              SlideTransition(
            position: Tween<Offset>(
              begin: const Offset(0, 0),
              end: Offset.zero,
            ).animate(animation),
            child: child,
          ),
        );
}

class FadeRoute extends PageRouteBuilder {
  final Widget? page;
  FadeRoute({this.page})
      : super(
          pageBuilder: (
            BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
          ) =>
              page!,
          transitionsBuilder: (
            BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
            Widget child,
          ) =>
              FadeTransition(
            opacity: animation,
            child: child,
          ),
        );
}
